package top.zxybird.common.constants;

import org.apache.commons.lang3.StringUtils;

public enum DeviceType {

  iphone("iPhone"), ipad("iPad"), ipod("iPod")/* 暂无 */, android("android"), chezai_android(
      "chezai_android"), androidpad(
          "androidpad"), wp("wp"), pc("pc"), web("web")/* 浏览器打开，mobile中不判断是否为web，供web使用的接口中指定 */, unknown("unknown");

  private String disName;

  DeviceType(String disName) {
    this.disName = disName;
  }

  public static DeviceType getTypeByName(String name) {
    for (DeviceType t : DeviceType.values()) {
      if (t.name().equalsIgnoreCase(name)) {
        return t;
      }
    }
    throw new IllegalArgumentException("no such deviceType,name:" + name);
  }

  /**
   * 如果不存在则返回null，不抛出异常。用于DeviceType不是必要的场景
   */
  public static DeviceType getTypeByNameWithNull(String name) {
    try {
      if (StringUtils.isEmpty(name)) {
        return null;
      }
      return getTypeByName(name);
    } catch (IllegalArgumentException e) {
      return null;
    }
  }

  public static DeviceType getTypeByNameWithUnknown(String name) {
    try {
      if (StringUtils.isEmpty(name)) {
        return DeviceType.unknown;
      }
      return getTypeByName(name);
    } catch (IllegalArgumentException e) {
      return DeviceType.unknown;
    }
  }

  public String getDisName() {
    return disName;
  }
}
